<?php

namespace App\Models\Ca;

use App\Facade\Code;
use App\Models\BaseModel;

/**
 * 客户管理
 */
class Customer extends BaseModel
{
    protected $table = 'ca__customers';

    protected $fillable = [
        'id',

        'is_public',
        'is_spend',
        'spend_time',
        'is_work_today',
        'last_visit_time',
        'last_visit_user_id',
        'effective',
        'customer_code',
        'consultation_type',
        'name',
        'gender',
        'birthday',
        'cost_money',
        'profession',
        'phone',
        'qq',
        'weixin',
        'age',
        'marital_status',
        'idcard',
        'mobile',
        'real_name',
        'is_type',
        'hf_time',
        'fb_time',
        'code',
        'creator_id',
        'consultation_way_id',
        'department_id',
        'source_id',
        'custom_service_id',
        'consultation_time',
        'yy_time',
        'yy_doctor_id',
        'yy_code',
        'yy_remark',
        'chat_record',
        'chat_file',
        'source_site',
        'gm_words',
        'source_ip',
        'source_entry',
        'source_consulting_page',
        'source_search_keywords',
        'source_matching_keywords',
        'jz_doctor_id',
        'jz_time',
        'jz_guide_doctor_id',
        'status',
        'is_hospitalized',
        'how_much_money',
        'come_way_id',
        'province_id',
        'city_id',
        'address',
        'consultation_remark',
        'doctor_remark',
        'input_ip',
        'member_id',
        'hospital_id',
        'old_id',
        'is_self_booking',
        'allot_status',
        'swt2',
        'illness',
        'species',
        'weixinlog',
        'phonelog',
        'tady_yy_time',
        'stage',
        'trust',
        'zy_time',
        'cy_time',
        'ls_time',
        'medical_id',
        'okswt',
        'okwx',
        'coordinate',
        'zhu_time',
        'chu_time',
        'zyday',
        'deleted_at',
        'updated_at',
        'created_at'
    ];

    protected $casts = [
        'disease_data' => 'array',
        'visit_data' => 'array',
        'is_public' => 'bool',
        'is_work_today' => 'bool'
    ];

    public function diseases()
    {
        return $this->hasMany(CustomerDisease::class, 'customer_id');
    }

    function getGenderTextAttribute($key)
    {
        return Code::text("AA", $key);
    }

    function getConsultationTypeTextAttribute($key)
    {
        return Code::text("AB", $key);
    }

    function getStageTextAttribute($key)
    {
        return Code::text("AE", $key);
    }

    function getStatusTextAttribute($key)
    {
        return Code::text("AC", $key);
    }
    function getEffectiveTextAttribute($key)
    {
        return Code::text("EF", $key);
    }
    
    function getMobileEncryptionAttribute()
    {
        return preg_replace('/(\d{3})\d{4}(\d{4})/', '$1****$2', $this->mobile);
    }
}